<template>
  <div v-loading="loading" style="height: 100%" element-loading-text="登录中..." />
</template>

<script>

  import { getLanguage } from '@/api/dev/language/sysLanguage'
  import { getTenant, getTenantInfo } from '@/api/login'

  export default {
    data() {
      return {
        code: undefined,
        tenantCode: undefined,
        loginForm: {},
        loading: true
      }
    },
    mounted() {
      this.code = this.$route.query.code
      this.tenantCode = this.$route.query.state
      this.init()
    },
    methods: {
      init() {
        if (this.code) {
          // 说明从飞书跳转
          this.$store.dispatch('FlyBookLogin', { code: this.code, tenantCode: this.tenantCode }).then(() => {
            this.loginForm.tenantCode = this.tenantCode
            if (this.tenantCode) {
              this.getLanguage()
            }
            this.$router.push({ path: '/index' || '/', name: '首页', params: { once: 1 }})
            this.$store.state.permission.leftActive = this.$store.state.permission.navActive = '999'
          })
        } else {
          // 没登录，需要跳转登录
          window.location.href = process.env.VUE_APP_BASE_API + '/fly/book/redirect/auth/pc' + (this.tenantCode ? '/' + this.tenantCode : '')
        }
      },
      getLanguage() {
        getLanguage(undefined, this.loginForm.tenantCode).then(response => {
          var localStorage = window.localStorage.getItem('language')
          if (localStorage === undefined || localStorage === null || localStorage === 'null') {
            localStorage = 'zh_cn'
          }
          this.$i18n.vm.messages[localStorage] = response.languageData
          // 修改国际化词条
          window.localStorage.setItem('language_' + localStorage, JSON.stringify(response.languageData))
          window.localStorage.setItem('language', localStorage)
          this.$i18n.locale = localStorage
        })
        this.getTenantInfo(this.loginForm.tenantCode)
      },
      getTenant() {
        this.formLoadIng = true
        getTenant().then(res => {
          if (res !== undefined) {
            this.tenant = res.data
          }
        })
      },
      // 获取当前租户的logo和名称
      getTenantInfo(tenantCode) {
        // 从url中获取租户code
        if (tenantCode === undefined) {
          tenantCode = this.$route.query.tenantCode
        }
        if (tenantCode !== undefined) {
          this.loginForm.tenantCode = tenantCode
        }
        getTenantInfo(this.loginForm.tenantCode).then(res => {
          if (res.data !== null) {
            this.tenantCompanyInfo.logo = res.data.productLogo
            this.tenantCompanyInfo.name = res.data.productName
            this.tenantCompanyInfo.width = res.data.productLogoWidth
            this.tenantCompanyInfo.height = res.data.productLogoHeight
          } else {
            this.tenantCompanyInfo = { logo: undefined, name: undefined, width: undefined, height: undefined }
          }
          debugger
          console.log(JSON.stringify(this.tenantCompanyInfo))
          window.localStorage.setItem('tenantCompanyInfo', JSON.stringify(this.tenantCompanyInfo))
          this.formLoadIng = false
        }).catch(() => {
          this.tenantCompanyInfo = { logo: undefined, name: undefined, width: undefined, height: undefined }
          this.formLoadIng = false
        })
      }
    }
  }
</script>

<style scoped>

</style>
